export default {
  calc_total (state) { // 计算总价格
    /* 所以已选中商品的 单价*数量总和 */
    let total = 0
    const { items } = state.cart
    const checkedItems = items.filter(item => item.checked)
    checkedItems.forEach(item => {
      total += item.minPrice * item.num
    })
    total = parseFloat(total.toFixed(2))
    state.cart.total = total
  },
  set_cart (state, cart) { // 更新购物车数据
    state.cart = cart
  },
  set_allChecked (state, allChecked) { // 改变全选状态
    state.cart.allChecked = allChecked
  },
  set_allChecked_all (state, allChecked) { // 点击全选 改变全选 和 单选装填
    state.cart.allChecked = allChecked
    state.cart.items.forEach(item => { item.checked = allChecked })
  },
  del_item (state, index) {
    state.cart.items.splice(index, 1)
  },
  add_cart (state, item) {
    state.cart.items.unshift(item)
  }
}
